<template>
    <div v-if="tend_id!='zk'">
        <el-button type="danger" size="small" @click="gotoList(3)" v-hasPermi="['crmsys:notAssignedCust:show']">未指派:{{
            notAssigned
        }}</el-button>
        <el-button type="warning" size="small" @click="gotoList(2)"
            v-hasPermi="['crmsys:notProcessedCustTotal:show']">总未处理:{{ totalNumAll
            }}</el-button>
        <el-button type="primary" size="small" @click="gotoList(1)" v-hasPermi="['crmsys:notProcessedCust:show']">未处理:{{
            totalNum
        }}</el-button>
    </div>
</template>

<script>
import {
    getNotProcessedCust,
    getNotProcessedCustTotal,
    getNotAssignedCust
} from "@/api/crmsys/custinfo/custinfo";
import { getToken, getTenant } from '@/utils/auth'
export default {
    name: "showNum",
    data() {
        return {
            totalNum: 0,//未处理总数
            myNum: 0,//我的客户
            reNum: 0,//再分配客户
            mustNum: 0,//必跟进客户
            totalNumAll: 0,//未处理总数 领导统计
            myNumAll: 0,//我的客户 领导统计
            reNumAll: 0,//再分配客户 领导统计
            mustNumAll: 0,//必跟进客户 领导统计
            notAssigned: 0,//未指派客户统计
            timer1: null,//定时器
            count: 0,
            maxCount: 10,//提示框执行时间 10分钟提醒一次
            notAssignedCust: false,//查看未指派数据权限
            notProcessedCustTotal: false,//查看未处理总数权限
            notProcessedCust: false,//查看未处理个人数量权限
            dateNow: null,//统计时间
            tend_id: null,//团队id
        }
    },
    created() {
        this.tend_id=getTenant();
        if ( this.tend_id== 'zk') {
            return;
        }
        this.getPermission();
        this.run();
        this.startTimer();
    },
    mounted() {

    },
    methods: {
        getPermission() {
            const permission = this.$store.state.user.permissions;
            if (permission.includes("*:*:*")) {
                //管理员
                this.notAssignedCust = true;
                this.notProcessedCustTotal = true;
                this.notProcessedCust = true;
            }

            if (permission.includes("crmsys:notAssignedCust:show")) {
                this.notAssignedCust = true;
            }
            if (permission.includes("crmsys:notProcessedCustTotal:show")) {
                this.notProcessedCustTotal = true;
            }
            if (permission.includes("crmsys:notProcessedCust:show")) {
                this.notProcessedCust = true;
            }

        },
        async getNotProcessedCust() {
            //获取未处理客户信息
            let count = await getNotProcessedCust(this.$store.state.user.id).then((response) => {
                this.totalNum = response.data.totalNum;
                this.myNum = response.data.myNum;
                this.reNum = response.data.reNum;
                this.mustNum = response.data.mustNum;
                if ((this.count == 0 || this.count >= this.maxCount) && this.totalNum > 0) {
                    this.showNotify('我的客户未处理',
                        '<div style="cursor:pointer;"><p>共计有[<b style="color:#ff4949">' + this.totalNum + '</b>]条数据未处理，其中：</p>' +
                        '<p>有[<b style="color:#ff4949">' + this.myNum + '</b>]条我的客户数据未处理</p>' +
                        '<p>有[<b style="color:#ff4949">' + this.reNum + '</b>]条再分配客户数据未处理</p>' +
                        '<p>有[<b style="color:#ff4949">' + this.mustNum + '</b>]条必跟进客户数据未处理</p>——————统计时间[' + this.dateNow + ']</div>',
                        '1');
                    if (this.myNum == 0) {
                        document.getElementById("我的客户").innerText = "";
                    } else {
                        document.getElementById("我的客户").innerText = this.myNum;
                    }
                    if (this.reNum == 0) {
                        document.getElementById("再分配客户").innerText = "";
                    } else {
                        document.getElementById("再分配客户").innerText = this.reNum;
                    }
                    if (this.mustNum == 0) {
                        document.getElementById("必跟进客户").innerText = "";
                    } else {
                        document.getElementById("必跟进客户").innerText = this.mustNum;
                    }
                    return 1;
                }
            });
            return count;
        },
        async getNotProcessedCustTotal() {
            //获取未处理客户信息总计
            let count = await getNotProcessedCustTotal(this.$store.state.user.id).then((response) => {
                this.totalNumAll = response.data.totalNum;
                this.myNumAll = response.data.myNum;
                this.reNumAll = response.data.reNum;
                this.mustNumAll = response.data.mustNum
                if ((this.count == 0 || this.count >= this.maxCount) && this.totalNumAll > 0) {
                    this.showNotify('团队未处理客户',
                        '<div style="cursor:pointer;"><p>共计有[<b style="color:#ff4949">' + this.totalNumAll + '</b>]条数据未处理，其中：</p>' +
                        '<p>共计有[<b style="color:#ff4949">' + this.myNumAll + '</b>]条团队客户数据未处理</p>' +
                        '<p>共计有[<b style="color:#ff4949">' + this.reNumAll + '</b>]条团队再分配客户数据未处理</p>' +
                        '<p>共计有[<b style="color:#ff4949">' + this.mustNumAll + '</b>]条团队必跟进客户数据未处理</p>——————统计时间[' + this.dateNow + ']</div>',
                        '2');
                }

                if (this.myNumAll == 0) {
                    document.getElementById("团队客户").innerText = "";
                } else {
                    document.getElementById("团队客户").innerText = this.myNumAll;
                }
                if (this.reNumAll == 0) {
                    document.getElementById("团队再分配客户").innerText = "";
                } else {
                    document.getElementById("团队再分配客户").innerText = this.reNumAll;
                }
                if (this.mustNumAll == 0) {
                    document.getElementById("团队必跟进客户").innerText = "";
                } else {
                    document.getElementById("团队必跟进客户").innerText = this.mustNumAll;
                }


                return 1;
            });
            return count;
        },
        async getNotAssignedCust() {
            //获取所有未指派数据
            let count = await getNotAssignedCust(this.$store.state.user.id).then((response) => {
                this.notAssigned = response.data.totalNum;
                if ((this.count == 0 || this.count >= this.maxCount) && this.notAssigned > 0) {
                    this.showNotify('未指派数据', '<div style="cursor:pointer;"><p>共计有[<b style="color:#ff4949">' + this.notAssigned + '</b>]条数据未指派</p>——————统计时间[' + this.dateNow + ']</div>', '3');
                }
                return 1;
            })
            return count;
        },
        startTimer() {
            //启动定时器
            this.timer1 = setInterval(() => {
                this.run();
            }, 1000 * 30);//1分钟执行一次
        },
        stopTimer() {
            //注销定时器
            window.clearInterval(this.timer1);
        },
        async run() {

            this.dateNow = this.getDateNow();
            let retult = "";
            if (this.notAssignedCust) {
                //查看未指派数据权限
                retult = await this.getNotAssignedCust();
            }
            if (this.notProcessedCustTotal) {
                //查看未处理总数权限
                retult = await this.getNotProcessedCustTotal();
            }
            if (this.notProcessedCust) {
                //查看未处理个人数量权限
                retult = await this.getNotProcessedCust();
            }
            this.count += 1;
            if (this.count > this.maxCount) {
                this.count = 1;
            }
        },
        showNotify(title, message, type) {
            let me = this;
            //打开提示框
            let notifyObj = this.$notify({
                title: title,
                dangerouslyUseHTMLString: true,
                message: message,
                duration: 1000 * 60 * (this.maxCount - 1),
                offset: 40,
                type: 'warning',
                position: 'bottom-right',
                onClick: function () {
                    let patch;
                    let params;
                    let title;
                    if (type == 1) {
                        //我的客户未处理
                        patch = "/crmsys/myCust";
                        params = {
                            t: Date.now(),
                            notProcessed: 1,
                            custOpType: "mycust"
                        };

                        title = "未处理客户";
                    }
                    if (type == 2) {
                        //团队未处理客户
                        patch = "/crmsys/teamCust";
                        params = {
                            t: Date.now(),
                            notProcessed: 1,
                            custOpType: "team1"
                        };

                        title = "团队未处理客户";
                    }
                    if (type == 3) {
                        //未指派数据
                        patch = "/crmsys/team";
                        params = {
                            t: Date.now()
                        };
                        title = "团队管理";

                    }
                    //跳转页面
                    me.$tab.openPage(
                        title,
                        patch,
                        params
                    );
                    notifyObj.close();
                }
            });
        },
        gotoList(type) {
            let patch;
            let params;
            let title;
            if (type == 1) {
                //我的客户未处理
                patch = "/crmsys/myCust";
                params = {
                    t: Date.now(),
                    notProcessed: 1,
                    custOpType: "mycust"
                };

                title = "未处理客户";
            }
            if (type == 2) {
                //团队未处理客户
                patch = "/crmsys/teamCust";
                params = {
                    t: Date.now(),
                    notProcessed: 1,
                    custOpType: "team1"
                };

                title = "团队未处理客户";
            }
            if (type == 3) {
                //未指派数据
                params = {
                    t: Date.now()
                };
                patch = "/crmsys/team";
                title = "团队管理";

            }
            //跳转页面
            this.$tab.openPage(
                title,
                patch,
                params
            );
        },
        getDateNow() {
            var now = new Date();
            var hours = now.getHours(); // 获取小时
            var minutes = now.getMinutes(); // 获取分钟
            var second = now.getSeconds(); // 获取秒
            return hours + ":" + minutes + ":" + second;
        }
    }, beforeDestroy() {
        //注销定时器
        this.stopTimer();
    }
}
</script>
<style lang="scss" scoped></style>
